#include <vector>
#include <string>
#include <iostream>
#include <rtk/luartk.h>
#include <rtk/rtk_utils.h>

using CPointerList = std::vector<void*>;
#ifdef _WIN32
using pid_t = uint32_t;
#endif
extern CPointerList searchProcessString(pid_t pid, const char* searchString, size_t searchStringLength);
extern int _returnPointerList(lua_State* L, const CPointerList& plist);

#define regfunc(n)                 \
	lua_pushliteral(L, #n);        \
	lua_pushcfunction(L, lua_##n); \
	lua_rawset(L, -3)

#define regconst(n)         \
	lua_pushliteral(L, #n); \
	lua_pushinteger(L, n);  \
	lua_rawset(L, -3)

extern std::vector<lua_CFunction>* get_init_funcs();

#define GAMEX_ADD_INIT_FUNC(func)                             \
	static int _add_##func()                                  \
	{                                                         \
		std::vector<lua_CFunction>* funcs = get_init_funcs(); \
		funcs->push_back(func);                               \
		return 0;                                             \
	}                                                         \
	static int _add_val = _add_##func();

extern std::vector<luaL_Reg>& get_gamex_lib_funcs();

#define GAMEX_ADD_FUNC(name, func)                                    \
	namespace {                                                       \
		struct CAdder {                                               \
			CAdder()                                                  \
			{                                                         \
				luaL_Reg reg = { name, func };                        \
				std::vector<luaL_Reg>& funcs = get_gamex_lib_funcs(); \
				funcs.push_back(reg);                                 \
			}                                                         \
		};                                                            \
		static CAdder adder;                                          \
	};

#define GAMEX_ADD_FUNC0(name) GAMEX_ADD_FUNC(#name, lua_##name)
